﻿#include "handldatabase.h"

#include <QSqlError>
#include <QApplication>
#include <QFile>
#include <QDebug>

#include "../commons/pinsinfors.h"

HandlDatabase  * HandlDatabase::m_handlDatabase = NULL;
HandlDatabase::HandlDatabase(QObject *parent) : QObject(parent)
  , m_numberIndex(32)
{
    initDatabase();
}

HandlDatabase::~HandlDatabase()
{

}

void HandlDatabase::initDatabase()
{
    m_listInforDatas.clear();
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<< QSqlDatabase::drivers();
#endif
    QString docParhFile =  QApplication::applicationDirPath();
    docParhFile +="/databases.db";

    m_db  = QSqlDatabase::addDatabase("QSQLITE");
    //数据库名memory 建立在内存中的数据库 该数据库只在程序运行期间有效
    m_db.setDatabaseName(docParhFile);
    //数据库连接测试
    if (!m_db.open()) {
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        return ;
    }
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    QSqlQuery query1;
    query1.exec("create table electricalMach (pins int primary key, times BIGINT)");
    QSqlQuery query2;
    query2.exec("create table pipelines (pins int primary key, times BIGINT)");

    QSqlQuery query3;
    query3.exec("create table relays (pins int primary key, times BIGINT)");

    QSqlQuery query4;
    query4.exec("create table pumps (pins int primary key, times BIGINT)");



    QSqlQuery query5;
    query5.exec("create table discharges (ids int primary key autoincrement, numbers BIGINT,stautes int)");

    QList<int>  lista = PinsInfors::gestance()->keyPins();

    //查询操作
    QSqlQuery query;
    QString sqlSelect = QString("select *from electricalMach;");
    if(!query.exec(sqlSelect)){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
#endif
    }else{


        while(query.next()){
            int pins = query.value("pins").toInt();
            int times = query.value("times").toInt();
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<pins;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<times;
#endif
            m_electricalMach.insert(pins,times);
        }
        QList<int>  listb = m_electricalMach.keys();
        for(int i =0 ;i < lista.size() ;i++){
            int itemsa =  lista.at(i);

            if(!listb.contains(itemsa)){
                m_electricalMach.insert(itemsa,0);
                QSqlQuery querys;
                QString sqlInsert = QString("insert into electricalMach(pins,times) values(%1,0);").arg(itemsa);
                if(!querys.exec(sqlInsert)){
#ifdef DEBUG_INFOR
                    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
#endif
                }

            }
        }
    }
    sqlSelect = QString("select *from pipelines;");
    if(!query.exec(sqlSelect)){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
#endif
    }else{


        while(query.next()){
            int pins = query.value("pins").toInt();
            int times = query.value("times").toInt();
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<pins;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<times;
#endif
            m_pipeline.insert(pins,times);
        }
        QList<int>  listb = m_pipeline.keys();
        for(int i =0 ;i < lista.size() ;i++){
            int itemsa =  lista.at(i);
            if(!listb.contains(itemsa)){
                m_pipeline.insert(itemsa,0);
                QSqlQuery querys;
                QString sqlInsert = QString("insert into pipelines(pins,times) values(%1,0);").arg(itemsa);
                if(!querys.exec(sqlInsert)){
#ifdef DEBUG_INFOR
                    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
#endif
                }

            }
        }
    }
    sqlSelect = QString("select *from relays;");
    if(!query.exec(sqlSelect)){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
#endif
    }else{


        while(query.next()){
            int pins = query.value("pins").toInt();
            int times = query.value("times").toInt();
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<pins;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<times;
#endif
            m_relays.insert(pins,times);
        }
        QList<int>  listb = m_relays.keys();
        for(int i =0 ;i < lista.size() ;i++){
            int itemsa =  lista.at(i);
            if(!listb.contains(itemsa)){
                m_relays.insert(itemsa,0);
                QSqlQuery querys;
                QString sqlInsert = QString("insert into relays(pins,times) values(%1,0);").arg(itemsa);
                if(!querys.exec(sqlInsert)){
#ifdef DEBUG_INFOR
                    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
#endif
                }

            }
        }
    }

    sqlSelect = QString("select *from pumps;");
    if(!query.exec(sqlSelect)){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
#endif
    }else{


        while(query.next()){
            int pins = query.value("pins").toInt();
            int times = query.value("times").toInt();
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<pins;
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<times;
#endif
            m_pumps.insert(pins,times);
        }
        QList<int>  listb = m_pumps.keys();
        for(int i =0 ;i < lista.size() ;i++){
            int itemsa =  lista.at(i);
            if(!listb.contains(itemsa)){
                m_pumps.insert(itemsa,0);
                QSqlQuery querys;
                QString sqlInsert = QString("insert into pumps(pins,times) values(%1,0);").arg(itemsa);
                if(!querys.exec(sqlInsert)){
#ifdef DEBUG_INFOR
                    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
#endif
                }

            }
        }
    }
    //   m_pipeline;
    //  m_relays;
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" m_electricalMach =="<< m_electricalMach;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" m_pipeline =="<< m_pipeline;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" m_relays =="<< m_relays;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" m_pumps =="<< m_pumps;
#endif
}

HandlDatabase *HandlDatabase::gestance()
{
    if(m_handlDatabase == NULL){
        m_handlDatabase = new HandlDatabase();
    }

    return m_handlDatabase;
}

void HandlDatabase::setElectricalMach(int pins, int times)
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" pins =="<< pins;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" times =="<< times;
#endif
    m_electricalMach.insert(pins,  times);

    QSqlQuery query;
    QString sqlUpdate = QString("update electricalMach set times=%1 where pins = %2;").arg(times).arg(pins);
    if(!query.exec(sqlUpdate)){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
#endif
    }


}

void HandlDatabase::setPipeline(int pins, int times)
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" pins =="<< pins;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" times =="<< times;
#endif
    m_pipeline.insert(pins,  times);

    QSqlQuery query;
    QString sqlUpdate = QString("update pipelines set times=%1 where pins = %2;").arg(times).arg(pins);
    if(!query.exec(sqlUpdate)){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
#endif
    }

}

void HandlDatabase::setRelays(int pins, int times)
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" pins =="<< pins;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" times =="<< times;
#endif
    m_relays.insert(pins,  times);
    QSqlQuery query;
    QString sqlUpdate = QString("update relays set times=%1 where pins = %2;").arg(times).arg(pins);
    if(!query.exec(sqlUpdate)){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
#endif
    }
}

void HandlDatabase::setPumps(int pins, int times)
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" pins =="<< pins;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<" times =="<< times;
#endif
    m_pumps.insert(pins,  times);
    QSqlQuery query;
    QString sqlUpdate = QString("update pumps set times=%1 where pins = %2;").arg(times).arg(pins);
    if(!query.exec(sqlUpdate)){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
#endif
    }
}

int HandlDatabase::getElectricalMach(int pins)
{
    int valuse = 0;
    valuse = m_electricalMach.value(pins,valuse);

    return valuse;
}

int HandlDatabase::getPipeline(int pins)
{
    int valuse = 0;
    valuse = m_pipeline.value(pins,valuse);

    return valuse;
}

int HandlDatabase::getRelays(int pins)
{
    int valuse = 0;
    valuse = m_relays.value(pins,valuse);

    return valuse;
}

int HandlDatabase::getPumps(int pins)
{
    int valuse = 0;
    valuse = m_pumps.value(pins,valuse);

    return valuse;
}

void HandlDatabase::setNumbersStautes(int numbers, int stautes)
{
    //    QSqlQuery query;
    //    QString sqlUpdate = QString("insert into discharges(numbers,stautes) values(%1, %2);").arg(numbers).arg(stautes);
    //    if(!query.exec(sqlUpdate)){
    //        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
    //    }
    QPair<int,int> pairs(numbers,  stautes);
    m_listInforDatas.append(pairs);
    if(m_listInforDatas.size() > m_numberIndex){
        m_listInforDatas.pop_front();
    }
}

int HandlDatabase::getNumbersStautes(int numbers)
{
    //    QSqlQuery query;
    //    QString sqlSelect = QString("select *from discharges where numbers = %1;").arg(numbers);
    //    if(!query.exec(sqlSelect)){
    //        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_db.lastError();
    //        return -1;
    //    }else{


    //        while(query.next()){
    //            int numbers = query.value("numbers").toInt();
    //            int stautes = query.value("stautes").toInt();
    //            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<numbers;
    //            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<stautes;
    //            return stautes;
    //        }
    //        return -1;
    //    }
    QPair<int,int> pairs ;
    for(int i =0 ; i < m_listInforDatas.size() ; i++){
        pairs = m_listInforDatas.at(i);
        if(pairs.first == numbers){

            return pairs.second;
        }
    }
    return -1;
}
